using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using IWCommunity.Common;
using Microsoft.SharePoint.Utilities;

namespace IWCommunity.Blogs.WebControls
{
    /// <summary>
    /// This control is designed to pull the list of tags from an XML file. This is different from the tag cloud control which pulls
    /// tags from the current web (site). This pulls from a pre-created XML file for performance reasons. It should be able to use
    /// the same XSLT as the tag cloud.
    /// </summary>
    public class SiteTags : List
    {

        private string _tagXmlFileUrl;
        //private string _tagPageUrl = "/search/results.aspx?v1=date&amp;k=tags:%22{0}%22"; //set default destination page as search results path

        ////not being used for now
        //public string TagPageURL
        //{
        //    get
        //    {
        //        return _tagPageUrl;
        //    }

        //    set
        //    {
        //        this._tagPageUrl = value;
        //    }
        //}


         public string TagXmlFileUrl
        {
            get
            {
                return _tagXmlFileUrl;
            }

            set
            {
                this._tagXmlFileUrl = value;
            }
        }


        protected override void OnLoad(EventArgs e)
        {
           

            if (System.Web.HttpContext.Current.Request.UserAgent.ToLower().Contains("robot"))
                this.Visible = false;

            base.OnLoad(e);
        }

        protected override System.Xml.XmlDocument BuildDocument()
        {
            try
            {
              

                //has the path to the XML file been set?               
                if (TagXmlFileUrl.Length == 0)
                    return null;

                XmlDocument oDoc = new XmlDocument();

              

                if (!TagXmlFileUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                    TagXmlFileUrl = SPUrlUtility.CombineUrl(Web.Site.RootWeb.Url, TagXmlFileUrl); //assume current web application root
                oDoc.LoadXml(Web.Site.RootWeb.GetFileAsString(TagXmlFileUrl));                                
                return oDoc;

            }
            catch (Exception ex)
            {
                Logger.LogError(ex); //log error
                XmlDocument oError = new XmlDocument();
                oError.LoadXml(string.Format("<Error>{0}</Error>", ex.Message));
                return oError;
            }
        }

    }
}
